package com.warmheart.base.config;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.warmheart.base.executor.MdcThreadPoolTaskExecutor;

/**
 * 
 * @ClassName: MyThreadPoolConfiguration
 * @Description: 线程池配置类
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@Configuration
@AutoConfigureAfter({ MyThreadPoolConfig.class })
public class MyThreadPoolConfiguration {

    @Bean
    public MdcThreadPoolTaskExecutor mdcThreadPoolTaskExecutor(MyThreadPoolConfig myThreadPoolConfig) {
        // 核心线程数CorePoolSize、最大线程数MaxPoolSize、线程空闲等待时间KeepAliveTime、
        // 线程空闲等待时间单位TimeUnit、任务等待队列的大小QueueCapacity
        MdcThreadPoolTaskExecutor taskExecutor = MdcThreadPoolTaskExecutor.newWithInheritedMdc(myThreadPoolConfig,
                TimeUnit.SECONDS, 100000);
        // 设置线程池内线程名称的前缀-------阿里编码规约推荐--方便出错后进行调试
        taskExecutor.setThreadNamePrefix("mdc-pool-");
        // 设置任务的拒绝策略
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        // 初始化
        taskExecutor.initialize();
        return taskExecutor;
    }
}
